package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.domain.FltVehiclePic;
import com.ruoyi.system.mapper.FltVehiclePicMapper;
import com.ruoyi.system.service.IFltVehiclePicService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 检测图片数据Service业务层处理
 *
 * @author fulu
 * @date 2023-09-13
 */
@Service
public class FltVehiclePicServiceImpl implements IFltVehiclePicService {
    @Autowired
    private FltVehiclePicMapper fltVehiclePicMapper;

    @Override
    public List<FltVehiclePic> getByCheckNo(String checkNo) {
        String yyyyMM = StringUtils.substring(checkNo, 0, 6);
        FltVehiclePic fltVehiclePic = new FltVehiclePic();
        fltVehiclePic.setTableName(fltVehiclePic.getTableName()+ "_" + yyyyMM);
        fltVehiclePic.setCheckNo(checkNo);
        List<FltVehiclePic> vehiclePicList = fltVehiclePicMapper.selectFltVehiclePicList(fltVehiclePic);
        for (FltVehiclePic file : vehiclePicList) {
            String nginxFileUrl = FileUtils.getNginxUrl(file.getFileUrl());
            file.setNginxUrl(nginxFileUrl);
        }
        return vehiclePicList;
    }

    @Override
    public List<FltVehiclePic> selectFltVehiclePicList(FltVehiclePic fltVehiclePic) {
        fltVehiclePic.setTableName("flt_vehicle_pic" + "_" + this.yyyyMM(fltVehiclePic.getCheckTime()));
        return fltVehiclePicMapper.selectFltVehiclePicList(fltVehiclePic);
    }

    @Override
    public int insertFltVehiclePic(FltVehiclePic fltVehiclePic) {
        fltVehiclePic.setTableName("flt_vehicle_pic" + "_" + this.yyyyMM(fltVehiclePic.getCheckTime()));
        return fltVehiclePicMapper.insertFltVehiclePic(fltVehiclePic);
    }

    private String yyyyMM(Date checkTime) {
        String yyyyMM = DateUtils.parseDateToStr("yyyyMM", checkTime);
        if (StringUtils.isBlank(yyyyMM)) {
            yyyyMM = DateUtils.dateTimeNow("yyyyMM");
        }
        return yyyyMM;
    }

}
